/**
 *
 * Copyright 2008 davethedev@googlemail.com
 * 
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


package nethelp.network;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

public class NetworkInterfaceTableModel extends AbstractTableModel {

    private Collection<NetworkInterfaceDetail> networkList = null;
    private String columnNames[] = { "Interface", "Name", "IP Address" };

    public NetworkInterfaceTableModel() {
        networkList = new ArrayList<NetworkInterfaceDetail>();
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();

                Enumeration ipAddresses = networkInterface.getInetAddresses();
                StringBuilder ipAddress = new StringBuilder();

                while (ipAddresses.hasMoreElements()) {
                    if (ipAddress.length() > 0) {
                        ipAddress.append(" / ");
                    }
                    InetAddress ip = (InetAddress) ipAddresses.nextElement();
                    ipAddress.append(ip.getHostAddress());
                }
                NetworkInterfaceDetail nid = new NetworkInterfaceDetail(networkInterface.getName(), networkInterface.getDisplayName(), ipAddress.toString());
                networkList.add(nid);
            }
        } catch (SocketException ex) {
            Logger.getLogger(NetworkInterfaceTableModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getRowCount() {
        return networkList.size();
    }

    public int getColumnCount() {
        return 3;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        NetworkInterfaceDetail detail = (NetworkInterfaceDetail) networkList.toArray()[row];
        if (col == 0) {
            return detail.getName();
        } else if (col == 1) {
            return detail.getDisplayName();
        } else if (col == 2) {
            return detail.getIpAddress();
        } else {
            return "";
        }
    }
}
